/*     */ package com.wgcloud.common;
/*     */ 
/*     */ import cn.hutool.json.JSONObject;
/*     */ import cn.hutool.json.JSONUtil;
/*     */ import com.jcraft.jsch.ChannelShell;
/*     */ import com.jcraft.jsch.JSch;
/*     */ import com.jcraft.jsch.JSchException;
/*     */ import com.jcraft.jsch.Session;
/*     */ import io.netty.channel.ChannelHandlerContext;
/*     */ import io.netty.channel.SimpleChannelInboundHandler;
/*     */ import io.netty.channel.group.ChannelGroup;
/*     */ import io.netty.channel.group.DefaultChannelGroup;
/*     */ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
/*     */ import io.netty.util.concurrent.EventExecutor;
/*     */ import io.netty.util.concurrent.GlobalEventExecutor;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class NettytHandler
/*     */   extends SimpleChannelInboundHandler<TextWebSocketFrame>
/*     */ {
/*  35 */   private static final Logger logger = LoggerFactory.getLogger(com.wgcloud.common.NettytHandler.class);
/*     */   
/*     */   public static final String HANDLE_OPERATE = "handle";
/*     */   
/*     */   public static final String HANDLE_VALUE = "value";
/*  40 */   public static final Integer CONNECTION_OUT = Integer.valueOf(30000);
/*     */   
/*     */   public static final String ENTER_VAL = "\r";
/*     */   
/*     */   public static final String LINE_NEXT_VAL = "\n";
/*  45 */   public static ChannelGroup channelGroup = (ChannelGroup)new DefaultChannelGroup((EventExecutor)GlobalEventExecutor.INSTANCE);
/*     */   
/*  47 */   public static Map<String, String> MAP_CMD = Collections.synchronizedMap(new HashMap<>());
/*     */ 
/*     */   
/*  50 */   public static Map<String, ChannelShell> MAP_SSH_SESSION = Collections.synchronizedMap(new HashMap<>());
/*     */ 
/*     */   
/*     */   protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
/*  54 */     String msgJSonStr = msg.text();
/*  55 */     if (StringUtils.isEmpty(msgJSonStr)) {
/*     */       return;
/*     */     }
/*  58 */     JSONObject msgJson = JSONUtil.parseObj(msgJSonStr);
/*  59 */     if ("connect".equals(msgJson.getStr("handle"))) {
/*     */       try {
/*  61 */         getSSHChannel(ctx.channel().id().toString(), msgJson.getStr("ip"), msgJson.getStr("user"), msgJson.getStr("pwd"), Integer.valueOf(msgJson.getStr("port")));
/*  62 */         executeCommand(ctx, "\r");
/*  63 */       } catch (JSchException e) {
/*  64 */         ctx.writeAndFlush(new TextWebSocketFrame("\n" + e.toString()));
/*  65 */         logger.error("ssh终端连接错误：", (Throwable)e);
/*     */       } 
/*  67 */     } else if ("cmd".equals(msgJson.getStr("handle"))) {
/*  68 */       String cmdStr = msgJson.getStr("value");
/*  69 */       if (StringUtils.isEmpty(MAP_CMD.get(ctx.channel().id().toString()))) {
/*  70 */         MAP_CMD.put(ctx.channel().id().toString(), cmdStr);
/*     */       } else {
/*  72 */         MAP_CMD.put(ctx.channel().id().toString(), (String)MAP_CMD.get(ctx.channel().id().toString()) + cmdStr);
/*     */       } 
/*  74 */       if (cmdStr.contains("\r") && 
/*  75 */         !StringUtils.isEmpty(MAP_CMD.get(ctx.channel().id().toString()))) {
/*  76 */         executeCommand(ctx, MAP_CMD.get(ctx.channel().id().toString()));
/*  77 */         MAP_CMD.remove(ctx.channel().id().toString());
/*     */       } 
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
/*  86 */     channelGroup.add(ctx.channel());
/*  87 */     logger.info(ctx.channel().remoteAddress() + "ssh终端上线了!");
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
/*  93 */     channelGroup.remove(ctx.channel());
/*  94 */     logger.info(ctx.channel().remoteAddress() + "ssh终端断开连接");
/*  95 */     ChannelShell channelShell = MAP_SSH_SESSION.get(ctx.channel().id().toString());
/*  96 */     if (channelShell != null) {
/*  97 */       Session session = channelShell.getSession();
/*  98 */       if (channelShell != null) {
/*  99 */         channelShell.disconnect();
/*     */       }
/* 101 */       if (session != null) {
/* 102 */         session.disconnect();
/*     */       }
/* 104 */       MAP_SSH_SESSION.remove(ctx.channel().id().toString());
/*     */     } 
/*     */   }
/*     */ 
/*     */   
/*     */   public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
/* 110 */     cause.printStackTrace();
/* 111 */     ctx.channel().close();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static ChannelShell getSSHChannel(String channelId, String host, String user, String password, Integer port) throws JSchException {
/* 125 */     ChannelShell channelShell = MAP_SSH_SESSION.get(channelId);
/* 126 */     if (channelShell != null) {
/* 127 */       return channelShell;
/*     */     }
/* 129 */     JSch jsch = new JSch();
/* 130 */     Session session = jsch.getSession(user, host, port.intValue());
/* 131 */     session.setPassword(password);
/*     */     
/* 133 */     session.setConfig("StrictHostKeyChecking", "no");
/* 134 */     session.connect(CONNECTION_OUT.intValue());
/*     */     
/* 136 */     session.setTimeout(600000);
/*     */     
/* 138 */     channelShell = (ChannelShell)session.openChannel("shell");
/* 139 */     channelShell.connect(CONNECTION_OUT.intValue());
/* 140 */     channelShell.setPtyType("dumb");
/* 141 */     channelShell.setPty(true);
/* 142 */     MAP_SSH_SESSION.put(channelId, channelShell);
/* 143 */     return channelShell;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static void executeCommand(ChannelHandlerContext ctx, String cmds) {
/* 153 */     ctx.writeAndFlush(new TextWebSocketFrame("\n"));
/*     */     try {
/* 155 */       ChannelShell channelShell = MAP_SSH_SESSION.get(ctx.channel().id().toString());
/* 156 */       if (null == channelShell) {
/* 157 */         ctx.writeAndFlush(new TextWebSocketFrame("\n\r~$ "));
/*     */         return;
/*     */       } 
/* 160 */       InputStream inputStream = channelShell.getInputStream();
/* 161 */       OutputStream outputStream = channelShell.getOutputStream();
/* 162 */       outputStream.write(cmds.getBytes());
/* 163 */       outputStream.flush();
/* 164 */       if (!StringUtils.isEmpty(cmds)) {
/* 165 */         logger.info(channelShell.getSession().getHost() + "，" + channelShell.getSession().getUserName() + "，执行ssh指令：" + cmds);
/*     */       }
/* 167 */       String showMsg = "";
/* 168 */       Thread.sleep(400L);
/* 169 */       byte[] tmp = new byte[4096];
/*     */       
/* 171 */       int beat = 0;
/*     */       
/*     */       while (true) {
/* 174 */         if (inputStream.available() > 0) {
/* 175 */           int i = inputStream.read(tmp);
/* 176 */           if (i >= 0) {
/*     */ 
/*     */             
/* 179 */             showMsg = new String(tmp, 0, i, "utf-8");
/* 180 */             ctx.writeAndFlush(new TextWebSocketFrame(showMsg));
/* 181 */             Thread.sleep(500L);
/*     */             continue;
/*     */           } 
/*     */         } 
/* 185 */         Thread.sleep(500L);
/*     */ 
/*     */         
/* 188 */         if (channelShell.isClosed()) {
/*     */           
/* 190 */           if (inputStream.available() > 0) {
/*     */             continue;
/*     */           }
/* 193 */           ctx.writeAndFlush(new TextWebSocketFrame("exit-status: " + channelShell.getExitStatus() + "，会话通道已超时请重新连接"));
/*     */           
/*     */           break;
/*     */         } 
/*     */         
/* 198 */         beat++;
/*     */         
/* 200 */         if (beat > 2) {
/*     */           break;
/*     */         }
/*     */       } 
/* 204 */     } catch (Exception e) {
/* 205 */       logger.error("shh指令执行错误：", e);
/*     */     } 
/*     */   }
/*     */ }


/* Location:              /Users/zhangyongxin/soft-install/wgcloud/wgcloud-v3.3.5/server/bank/wgcloud-server-release.jar!/BOOT-INF/classes/com/wgcloud/common/NettytHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */